package net.akcegaraz.tipleague.client;

import net.akcegaraz.tipleague.client.event.CreateTicketEvent;
import net.akcegaraz.tipleague.client.event.CreateTicketEventHandler;
import net.akcegaraz.tipleague.client.event.HelloEvent;
import net.akcegaraz.tipleague.client.event.HelloEventHandler;
import net.akcegaraz.tipleague.client.event.TicketCreatedEvent;
import net.akcegaraz.tipleague.client.event.TicketCreatedEventHandler;
import net.akcegaraz.tipleague.client.event.TicketShownEvent;
import net.akcegaraz.tipleague.client.event.TicketShownEventHandler;
import net.akcegaraz.tipleague.client.presenter.EditTicketPresenter;
import net.akcegaraz.tipleague.client.presenter.HelloPresenter;
import net.akcegaraz.tipleague.client.presenter.Presenter;
import net.akcegaraz.tipleague.client.presenter.RoundPresenter;
import net.akcegaraz.tipleague.client.service.TipServiceAsync;
import net.akcegaraz.tipleague.client.view.EditTicketView;
import net.akcegaraz.tipleague.client.view.HelloView;
import net.akcegaraz.tipleague.client.view.RoundView;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasWidgets;

public class AppController implements Presenter, ValueChangeHandler<String> {
    private static final String CREATE_TICKET = "createTicket";
    private static final String LIST_TICKETS = "listTickets";
    private static final String SHOW_TICKET = "showTicket";
    private static final String HELLO = "hello";
    private final HandlerManager eventBus;
    private final TipServiceAsync rpcService;
    private HasWidgets container;

    public AppController(TipServiceAsync rpcService, HandlerManager eventBus) {
	this.eventBus = eventBus;
	this.rpcService = rpcService;
	bind();
    }

    public void go(HasWidgets container) {
	this.container = container;

	if ("".equals(History.getToken())) {
	    History.newItem(CREATE_TICKET);
	} else {
	    History.fireCurrentHistoryState();
	}
    }

    public void onValueChange(ValueChangeEvent<String> event) {
	String token = event.getValue();

	if (token != null) {
	    Presenter presenter = null;

	    if (token.equals(CREATE_TICKET)) {
		presenter = new EditTicketPresenter(rpcService, eventBus,
			new EditTicketView());
	    } else if (token.equals(LIST_TICKETS)) {
		presenter = new RoundPresenter(rpcService, eventBus,
			new RoundView());
	    } else if (token.equals(SHOW_TICKET)) {
		presenter = new EditTicketPresenter(rpcService, eventBus,
			new EditTicketView());
	    } else if (token.equals(HELLO)) {
		presenter = new HelloPresenter(rpcService, eventBus,
			new HelloView());
	    }

	    if (presenter != null) {
		presenter.go(container);
	    }
	}
    }

    private void bind() {
	History.addValueChangeHandler(this);

	eventBus.addHandler(CreateTicketEvent.TYPE,
		new CreateTicketEventHandler() {
		    public void onCreateTicket(CreateTicketEvent event) {
			doCreateTicket();
		    }
		});

	eventBus.addHandler(TicketCreatedEvent.TYPE,
		new TicketCreatedEventHandler() {
		    public void onTicketCreated(TicketCreatedEvent event) {
			doShowTicket();
		    }
		});

	eventBus.addHandler(TicketShownEvent.TYPE,
		new TicketShownEventHandler() {
		    public void onTicketShown(TicketShownEvent event) {
			doTicketShown();
		    }
		});

	eventBus.addHandler(HelloEvent.TYPE, new HelloEventHandler() {
	    public void onHello(HelloEvent event) {
		doHello();
	    }
	});
    }

    private void doCreateTicket() {
	History.newItem(CREATE_TICKET);
    }

    private void doHello() {
	History.newItem(HELLO);
    }

    private void doShowTicket() {
	History.newItem(SHOW_TICKET);
    }

    private void doTicketShown() {
	History.newItem(LIST_TICKETS);
    }
}
